<?php
class model_newagk_labels extends gdl_components_basemodel 
{

    public function primarykey() {
        return 'id';
    }

    public function tableName() {
        return 'agk_labels';
    }
	
	public function getInfoArgOne($arrg=[],$field)
    {
		if(empty($arrg)) return false;
		return $this->selectOne($arrg, $field);
    }
	public function batchList($id){
		$batchList = $this->getInfoArg('id in('.implode(',',$id).') or pid in('.implode(',',$id).')','id,name,pid','order by id desc');
		$list = [];
		foreach($batchList as $v){
			if($v['pid']){
				$list['tagSelect'][] = ['id'=>$v['id'],'name'=>$v['name'],'pid'=>$v['pid']];
			} else {
				$list['tag'] = ['id'=>$v['id'],'name'=>$v['name']];
			}
		}
		
		return $list;
		
	}
	
	public function getInfoArg($arrg=[],$field)
    {
		if(empty($arrg)) return false;
		return $this->select($arrg, $field)->items;
    }
	

	public function delEmpty($orgId,$id,$reg)
    {
		if(empty($orgId) || !is_array($orgId) || !$id) return false;
		$label_id = $this->getInfoArg(['pid'=>$id,'admin_reg'=>$reg,'types'=>1],'id');
		$delId=[0];
		foreach($label_id as $v){
			if(!in_array($v['id'],$orgId)){
				$delId[] = $v['id'];
			}
		}
		$this->delete('pid='.$id." and admin_reg='".$reg."' and types=1 and id in(".implode(',',$delId).')');
    }
	
	//通过id 获取标签组 用于试卷详情的
	public function getInfoArgDetails($label_id=0,$label_value=[],$reg='')
    {
		if(empty($reg) && !$label_id) return false;
		if(!$label_id){
			$list = $this->getInfoArg("admin_reg in('".implode("','",$reg)."')", 'id,name,pid');
		} else {
			$pids = substr($label_id,2);
			$conid = 'id in('.$label_id.')';
			if($pids!=''){
				$conid .=  ' or pid in('.$pids.')';
			}
			$list = $this->getInfoArg($conid, 'id,name,pid');
		}
		$labelId = [];
		
		foreach($list as $v){
			if($v['pid']){
				$labelId['tagSelect'][] = ['id'=>$v['id'],'name'=>$v['name'],'pid'=>$v['pid'],'select'=>in_array($v['id'],$label_value) ? 'select' : 'no'];
			} else {
				$labelId['tag'][] = ['id'=>$v['id'],'name'=>$v['name']];
			}
		}
		 return $labelId;
    }
	
	
	
	
}
